package vip.xiaonuo.biz.modular.article.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.biz.modular.article.entity.BizArticle;
import vip.xiaonuo.biz.modular.article.mapper.BizArticleMapper;
import vip.xiaonuo.biz.modular.article.param.BizArticleAddParam;
import vip.xiaonuo.biz.modular.article.param.BizArticleEditParam;
import vip.xiaonuo.biz.modular.article.param.BizArticleIdParam;
import vip.xiaonuo.biz.modular.article.param.BizArticlePageParam;
import vip.xiaonuo.biz.modular.article.service.BizArticleService;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BizArticleServiceImpl extends ServiceImpl<BizArticleMapper, BizArticle> implements BizArticleService {


    @Override
    public void add(BizArticleAddParam addParam) {
        BizArticle article = BeanUtil.copyProperties(addParam, BizArticle.class);
        this.save(article);
    }

    @Override
    public void edit(BizArticleEditParam editParam) {
        BizArticle article = BeanUtil.copyProperties(editParam, BizArticle.class);
        this.updateById(article);
    }

    @Override
    public void delete(List<BizArticleIdParam> idListParam) {
        this.removeBatchByIds(idListParam.stream().map(BizArticleIdParam::getId).collect(Collectors.toList()));
    }

    @Override
    public Page<BizArticle> page(BizArticlePageParam pageParam) {
        LambdaQueryWrapper<BizArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ObjectUtil.isNotNull(pageParam.getColumnId()),BizArticle::getColumnId, pageParam.getColumnId());
        wrapper.like(ObjectUtil.isNotNull(pageParam.getTitle()),BizArticle::getTitle, pageParam.getTitle());
        wrapper.orderByAsc(BizArticle::getViewCount);
        return this.page(CommonPageRequest.defaultPage(), wrapper);
    }

    @Override
    public Page<BizArticle> pageForC(BizArticlePageParam pageParam) {
        LambdaQueryWrapper<BizArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BizArticle::getVisible,"SHOW");
        wrapper.eq(ObjectUtil.isNotNull(pageParam.getColumnId()),BizArticle::getColumnId,pageParam.getColumnId());
        wrapper.like(ObjectUtil.isNotNull(pageParam.getTitle()),BizArticle::getTitle,pageParam.getTitle());
        wrapper.orderByAsc(BizArticle::getViewCount);
        return this.page(CommonPageRequest.defaultPage(), wrapper);
    }

    @Transactional
    @Override
    public BizArticle detail(BizArticleIdParam idParam) {
        BizArticle article = this.queryEntity(idParam.getId());
        // 增加浏览量
        article.setViewCount(article.getViewCount()+1);
        this.updateById(article);
        return article;
    }

    @Override
    public BizArticle queryEntity(String id) {
        BizArticle article = this.getById(id);
        if (ObjectUtil.isNull(article)){
            throw new CommonException("文章不存在，id值为{}",id);
        }
        return article;
    }

    @Override
    public List<BizArticle> getLatestArticle() {
        LambdaQueryWrapper<BizArticle> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BizArticle::getVisible,"SHOW");
        wrapper.orderByDesc(BizArticle::getCreateTime);
        wrapper.last("limit 20");
        return this.list(wrapper);
    }

    @Override
    public void editVisible(BizArticleEditParam editParam) {
        LambdaUpdateWrapper<BizArticle> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(BizArticle::getVisible,editParam.getVisible());
        wrapper.eq(BizArticle::getId,editParam.getId());
        this.update(wrapper);
    }
}
